<!--#include file="conn.asp"-->
<!--#include file="chkuser.asp"-->
<%
call adminRight(9)

action=request("action")
bakName=trim(request.Form("bakName"))

if SqlDriverType=1 then
	db="../"&relPath&AccessName
	if lcase(right(bakName,4))<>".mdb" then
		bakName="../"&dbBakSavePath&bakName&".mdb"
	else
		bakName="../"&dbBakSavePath&bakName
	end if
elseif SqlDriverType=2 then
	if lcase(right(bakName,4))<>".bak" then
		bakName="../"&dbBakSavePath&bakName&".bak"
	else
		bakName="../"&dbBakSavePath&bakName
	end if
end if

if action<>"" then response.Write("<link href='style.css' rel='stylesheet' type='text/css' />")

'*********************ACCESS备份还原程序Start*********************
if action="AccessBackUp" then
	if CopyFileTo(db,bakName)=True then
		WriteSuccessMsg("<div class='icoBtn_do_i'><span class='btnIco btnChange'></span><font class='light_green'>数据库已备份，备份文件名称"&bakName&"！</font></div>")
		response.End()
	else
		WriteErrMsg("<div class='icoBtn_do_i'><span class='btnIco btnDel'></span><font class='light_red'>数据库备份失败！</font></div>")
		response.End()
	end if
end if

if action="AccessRestore" then
	if CopyFileTo(bakName,db)=True then
		WriteSuccessMsg("<div class='icoBtn_do_i'><span class='btnIco btnChange'></span><font class='light_green'>数据库已还原成功，原备份文件名称"&bakName&"！</font></div>")
		response.End()
	else
		WriteErrMsg("<div class='icoBtn_do_i'><span class='btnIco btnDel'></span><font class='light_red'>数据库还原失败！</font></div>")
		response.End()
	end if
end if

if action="AccessRepair" then
	call CopyFileTo(db,bakName)
	if Repair()=True then
		WriteSuccessMsg("<div class='icoBtn_do_i'><span class='btnIco btnChange'></span><font class='light_green'>数据库已备份，原数据库已压缩修复成功！</font></div>")
		response.End()
	else
		WriteErrMsg("<div class='icoBtn_do_i'><span class='btnIco btnDel'></span><font class='light_red'>数据库压缩修复失败！</font></div>")
		response.End()
	end if
end if

function Repair()
    '开始压缩
    Set Engine = Server.CreateObject("JRO.JetEngine") 
    prov = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" 
    Engine.CompactDatabase prov & server.MapPath(db), prov & server.MapPath("../"&relPath&"Temp_"&AccessName) 
    set Engine = nothing 
	call CopyFileTo("../"&relPath&"Temp_"&AccessName,db)
	call DelFile("../"&relPath&"Temp_"&AccessName)
	if err then
		Repair=False
	else
		Repair=True
	end if
	err.clear
end function
'*********************ACCESS备份还原程序End*********************

'*********************MSSQL备份还原程序Start*********************
if action="MSSQLBackUp" then
	Set srv=Server.CreateObject("SQLDMO.SQLServer")
	srv.LoginTimeout = 15
	srv.Connect SqlLocalName, SqlUID, SqlPWD
	Set bak = Server.CreateObject("SQLDMO.Backup")
	bak.Database=DatabaseName
	bak.Devices=Files
	bak.Files=server.MapPath(bakName)
	bak.SQLBackup srv
	if err then
		WriteErrMsg("<div class='icoBtn_do_i'><span class='btnIco btnDel'></span><font class='light_red'>数据库备份失败："&err.Description &"</font></div>")
		response.End()
	else
		WriteSuccessMsg("<div class='icoBtn_do_i'><span class='btnIco btnChange'></span><font class='light_green'>数据库备份成功，备份文件名称："&bakName&"！</font></div>")
		response.End()
	end if 
end if

if action="MSSQLRestore" then
	Set srv=Server.CreateObject("SQLDMO.SQLServer") 
	srv.LoginTimeout = 15 
	srv.Connect SqlLocalName, SqlUID, SqlPWD 
	Set rest=Server.CreateObject("SQLDMO.Restore") 
	rest.Action=0
	rest.Database=DatabaseName 
	rest.Devices=Files 
	rest.Files=server.MapPath(bakName) 
	rest.ReplaceDatabase=True
	rest.SQLRestore srv 
	if err then 
		WriteErrMsg("<div class='icoBtn_do_i'><span class='btnIco btnDel'></span><font class='light_red'>数据库还原失败："&err.Description &"</font></div>")
		response.End()
	else
		WriteSuccessMsg("<div class='icoBtn_do_i'><span class='btnIco btnChange'></span><font class='light_green'>数据库还原成功！原备份文件名称："&bakName&"</font></div>")
		response.End()
	end if 
end if
'*********************MSSQL备份还原程序End*********************
%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>备份与还原数据库</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script language="javascript">
function checkAccessRestore(f){
	if(f.bakName.value=='请填写数据库备份文件的名称' || f.bakName.value==''){
		alert('请输入备份文件名称。');
		f.bakName.focus();
		return false;
	}
}
function checkAccessRepair(f){
	if(f.bakName.value=='为了避免出错：请填写数据库临时备份名称' || f.bakName.value==''){
		alert('为了避免出错，请填写数据库临时备份名称。');
		f.bakName.focus();
		return false;
	}
}
function checkMSSQLRestore(f){
	if(f.bakName.value=='请填写数据库备份文件的名称' || f.bakName.value==''){
		alert('请输入备份文件名称。');
		f.bakName.focus();
		return false;
	}
}
</script>
</head>
<body>
    <div id="frame_Toolbar" class="toolbarBg"> 
		<a href="javascript:self.history.back();" class="icoBtn_ct"><span class="btnIco btnBack"></span>返回</a> 
		<a href="javascript:self.location.reload();" class="icoBtn_ct"><span class="btnIco btnReload"></span>刷新</a> 
		<span class="split ftSplit" ></span>
      <div class="pageInfo right light_gray"></div>
    </div>
 <table class="cTable_2 table">
	<tr class="cTitle toolbarBg">
	  <td colspan="2"><div class="cLeftPadding">备份与还原数据库</div></td>
	</tr>
	<tr>
	  <td width="20%" class="tRight">当前使用数据库类型：</td>
	  <td><%
	  if SqlDriverType=1 then 
	  Response.Write("Access (*.mdb)") 
	  elseif SqlDriverType=2 then 
	  Response.Write("MSSQL (Microsoft SQL Server)") 
	  end if
	  %></td>
	</tr>
	<tr>
	  <td width="20%" class="tRight">使用说明：</td>
	  <td>
	  <div class="left" style="width:500px; line-height:150%;color:#FF0000;">
		  ① 数据库备份文件存放于网站根目录下的<%=dbBakSavePath%>文件夹里。备份名称可自定(为了安全，名称勿过于简单！)。请记住您的备份文件名称，以便恢复数据库时使用!<br>
		  ② 如果备份数据库文件后，将现使用的数据库中管理员用户名或密码修改，还原后可能导致后台无法进入，所以备份数据库文件时请先牢记备份时的用户名及密码！<br>
		  ③ 数据库还原后数据库中数据会恢复到备份时的数据！为了不必要的还原的，您可以请先备份当前使用的数据库，以便恢复不必要的还原！<br>
		  ④ 由于用户错误的还原而造成数据丢失，后果一切自负！<br>
	  </div>
	  </td>
	</tr>
	<%if SqlDriverType=1 then%>
	<form action="?action=AccessBackUp" method="post">	
	<tr>
	  <td class="tRight">备份：</td>
	  <td>
	  <input name="bakName" type="text" value="<%=year(now)&month(now)&day(now)&hour(now)&Minute(now)&Second(now)%>" size="50"> 
	  <input type="submit" value="开始备份" class="button">
	  </td>
	</tr>
	</form>
	<form action="?action=AccessRestore" method="post" onSubmit="return checkAccessRestore(this)">	
	<tr>
      <td class="tRight">还原：</td>
	  <td>
	  <input name="bakName" type="text" onFocus="this.value=''" value="请填写数据库备份文件的名称" size="50">
      <input type="submit" value="开始还原" class="button">
	  </td>
	</tr>
	</form>
	<form action="?action=AccessRepair" method="post" onSubmit="return checkAccessRepair(this)">	
	<tr>
      <td class="tRight">压缩修复Access数据库：</td>
	  <td>
	  <input name="bakName" type="text" onFocus="this.value=''" value="为了避免出错：请填写数据库临时备份名称" size="50">
	  <input type="submit" value="开始压缩修复" class="button">
	  </td>
	</tr>
    </form>
	<%else%>
	<form action="?action=MSSQLBackUp" method="post">	
	<tr>
	  <td class="tRight">备份：</td>
	  <td>
	  <input name="bakName" type="text" value="<%=year(now)&month(now)&day(now)&hour(now)&Minute(now)&Second(now)%>" size="50"> 
	  <input type="submit" value="开始备份" class="button">
	  </td>
	</tr>
	</form>
	<form action="?action=MSSQLRestore" method="post" onSubmit="return checkMSSQLRestore(this)">	
	<tr>
      <td class="tRight">还原：</td>
	  <td>
	  <input name="bakName" type="text" onFocus="this.value=''" value="请填写数据库备份文件的名称" size="50">
      <input type="submit" value="开始还原" class="button">
	  </td>
	</tr>
	</form>
	<%end if%>
  </table>
    <div id="cEndToolbar" class="toolbarBg">
      <div class="caButCase">
      </div>
    </div>
</body>
</html>